﻿using Sys.Common;
using Sys.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sys.Core.Sys
{
    /// <summary>
    /// 用户角色业务
    /// </summary>
    public class UserRoleBll : ExampleBase<SysUserRole, UserRoleBll>
    {
        /// <summary>
        /// 根据用户编号获取列表
        /// </summary>
        /// <param name="id">用户编号</param>
        /// <returns>实体列表</returns>
        public IQueryable<SysUserRole> EntitiesByUserID(string id)
        {
            return Entities.Where(m => m.UserID.Equals(id));
        }

        /// <summary>
        /// 分配角色
        /// </summary>
        /// <param name="id">用户编号</param>
        /// <param name="data">分配的数据集合</param>
        /// <returns>操作结果</returns>
        public bool Assign(string id, string data)
        {
            string[] rlist = data.Split(',');

            var list = new List<SysUserRole>();

            foreach (string r in rlist)
            {
                if (!string.IsNullOrEmpty(r))
                {
                    var item = new SysUserRole();

                    item.ID = StringHelper.GetGuid();
                    item.UserID = id;
                    item.RoleID = r;

                    list.Add(item);
                }
            }

            Delete(m => m.UserID.Equals(id));

            return Insert(list) == list.Count;
        }
    }
}
